package com.litong.rms.action;


import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import com.litong.rms.bean.Auth;
import com.litong.rms.bean.Function;
import com.litong.rms.bean.Module;
import com.litong.rms.service.AuthService;
import com.litong.rms.service.FunctionService;
import com.litong.rms.service.ModuleService;


public class ModuleAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -883118110323968234L;
	
	private Module module;//模块实例
	
	private Auth auth;//权限实例
	
	private String moduleId;//模块ID
	
	//private String roleId;//角色ID
	
	private String[] functionIdArray;//选中的功能ID
	
	private int[] selectedFunctionId;//已经赋予的功能ID
	
	private ModuleService moduleService;//模块服务
	
	private FunctionService functionService;//功能服务
	
	private AuthService authService;//权限服务
	
	private List<Module> moduleList;//模块列表
	
	private List<Function> functionList;//功能列表
	
	private List<Auth> authList;//权限列表
	
	private List<String> lstTree;//构造存放树list
	
	private JSONArray dataArray ;//将lstTree转换成json格式的实体
	
	private String nodeSelect;

	public List<String> getLstTree() {
		return lstTree;
	}

	public void setLstTree(List<String> lstTree) {
		this.lstTree = lstTree;
	}

	/*获得模块列表*/
	public String list(){
		moduleList = moduleService.getModuleList();
		functionList = functionService.getFunctionList();
		buildTree();
		return SUCCESS;
	}
	//得到树形展现
	public String buildTree(){
		moduleList = moduleService.getModuleList();
		functionList = functionService.getFunctionList();
		if(functionList==null||moduleList==null){
			return SUCCESS;
		}
		String str = null;
		lstTree = new ArrayList<String>(); 
		
		/*for(int i=0;i<functionList.size();i++){
			Function func = functionList.get(i);
			str = "{id:"+func.getFunctionId()+", pId:1, name:\""+func.getFunctionName()+"\",open:true}";
			fList.add(str);
			//System.out.println(str);
		}*/
		for(int i=0;i<moduleList.size();i++){
			Module modu = moduleList.get(i);
			str = "{id:"+modu.getModuleId()+", pId:0, name:\""+modu.getModuleName()+"\",open:false}";
			lstTree.add(str);
			for(int j=0;j<functionList.size();j++){
				Function func = functionList.get(j);
				authList = authService.getAuthList(modu.getModuleId());
				selectedFucntion(authList);//得到已近分配到的功能
				boolean b = false;
				for(int n=0;n<selectedFunctionId.length;n++){
					//判断是否已经分配功能如果已经分配标记为checked
					b = false;
					if(selectedFunctionId[n]==func.getFunctionId()){
						b = true;
						n = selectedFunctionId.length;
					}
				}
				str = "{id:"+func.getFunctionId()+"abc"+", pId:"+modu.getModuleId()+", name:\""+func.getFunctionName()+"\",checked:"+b+",open:false}";			
				lstTree.add(str);
				//System.out.println(str);
			}
		}
		
        dataArray = JSONArray.fromObject(lstTree);
        return SUCCESS;
		
	}
	
	/*添加模块*/
	public String addModule(){
		
		moduleService.addModule(module);//添加模块
		//为模块分配功能--将模块id功能id插入auth表
		if(functionIdArray!=null){
			auth = new Auth();
			for(int i = 0;i<functionIdArray.length;i++){
				auth.setModuleId(module.getModuleId());
				auth.setFunctionId(Integer.parseInt(functionIdArray[i]));
				authService.addAuth(auth);
			}
		}
		return 	SUCCESS;
	};
	
	public String findModule(){
		
		moduleService.findModule(module);
		return SUCCESS;
	}
	
	
	/*删除模块*/
	public String deleteModule(){
		module = new Module();
		module.setModuleId(Integer.parseInt(moduleId));
		//首先删除权限表中的数据（根据模块id删除关于这个模块所有功能）保持数据一致性
		authService.deleteAuth("module", Integer.parseInt(moduleId));
		//再删出当前模块
		moduleService.deleteModule(module);
		buildTree();
		return list();
	}
	
	
	/*查找根据ID返回模块*/
	public String findUpdateModule(){
		module = moduleService.findById(Integer.parseInt(moduleId));
		//根据模块ID查询模块已经分配的功能
		
		authList = authService.getAuthList(Integer.parseInt(moduleId));
		selectedFucntion(authList);
		/*if(authList!=null||authList.isEmpty()){
			selectedFunctionId = new int[authList.size()];
			for(int i=0;i<authList.size();i++){
				auth = authList.get(i);
				selectedFunctionId[i] = auth.getFunctionId();
			}
		}*/
		//加载authList
		list();
		return SUCCESS;
	}
	
	
	//查询模块已经分配的功能
	@SuppressWarnings("null")
	public void selectedFucntion(List<Auth> authList){
		
		if(authList!=null||authList.isEmpty()){
			selectedFunctionId = new int[authList.size()];
			for(int i=0;i<authList.size();i++){
				auth = authList.get(i);
				selectedFunctionId[i] = auth.getFunctionId();
			}
		}
		
	}
	/*修改模块*/
	public String updateModule(){
		//首先删除根据模块Id删除相关的模块所有功能在更新插入新的功能
		authService.deleteAuth("module", module.getModuleId());
		if(functionIdArray!=null){
			auth = new Auth();
			for(int i = 0;i<functionIdArray.length;i++){
				auth.setModuleId(module.getModuleId());
				auth.setFunctionId(Integer.parseInt(functionIdArray[i]));
				authService.addAuth(auth);
			}
		}
		moduleService.updateModule(module);
		return SUCCESS;
	}
	
	/*根据树形结构更新模块功能*/
	public String updateByTree(){
		
		String[] str1 = nodeSelect.split(",");
		String s1,s2;
		auth = new Auth();
		for(int i = 0;i<str1.length;i++){
			String[] str2 = str1[i].split(":");
			
			if(str2[0].length()>3){
				s1 = str2[0].substring(0, str2[0].length()-3).trim();
				s2 = str2[1].trim();
				auth.setFunctionId(Integer.parseInt(s1));
				auth.setModuleId(Integer.parseInt(s2));
				
				if(authService.findAuth(auth)==null){
					
					authService.addAuth(auth);
				}else{
					authService.deleteAuth(s2, Integer.parseInt(s1));
				}
			}
			
		}
		//System.out.println(str1.toString());
		return SUCCESS;
	}
	
	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public String getModuleId() {
		return moduleId;
	}

	public void setModuleId(String moduleId) {
		this.moduleId = moduleId;
	}

	/*public ModuleService getModuleService() {
		return moduleService;
	}*/

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public List<Module> getModuleList() {
		return moduleList;
	}

	public void setModuleList(List<Module> moduleList) {
		this.moduleList = moduleList;
	}

	public void setFunctionList(List<Function> functionList) {
		this.functionList = functionList;
	}

	public List<Function> getFunctionList() {
		return functionList;
	}

	/*public FunctionService getFunctionService() {
		return functionService;
	}*/

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	public void setFunctionIdArray(String[] functionIdArray) {
		this.functionIdArray = functionIdArray;
	}

	public String[] getFunctionIdArray() {
		return functionIdArray;
	}

	public void setAuth(Auth auth) {
		this.auth = auth;
	}

	public Auth getAuth() {
		return auth;
	}

	public void setAuthService(AuthService authService) {
		this.authService = authService;
	}

	/*public AuthService getAuthService() {
		return authService;
	}*/

	public void setAuthList(List<Auth> authList) {
		this.authList = authList;
	}

	public List<Auth> getAuthList() {
		return authList;
	}

	public void setSelectedFunctionId(int[] selectedFunctionId) {
		this.selectedFunctionId = selectedFunctionId;
	}

	public int[] getSelectedFunctionId() {
		return selectedFunctionId;
	}

	public void setDataArray(JSONArray dataArray) {
		this.dataArray = dataArray;
	}

	public JSONArray getDataArray() {
		return dataArray;
	}

	public void setNodeSelect(String nodeSelect) {
		this.nodeSelect = nodeSelect;
	}

	public String getNodeSelect() {
		return nodeSelect;
	}

	

	
	
	

}
